package br.com.massai.view.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "competenciaConverter")
public class CompetenciaConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2==null || arg2.equals("") || arg2.equals("__/____"))
			return null;
		else{
			String competencia = arg2.replace("/", "");
			return competencia;
		}
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if(arg2 != null){
			StringBuilder competencia = new StringBuilder();
			String competenciasm = arg2.toString();
			if(competenciasm.length() == 6)
				competencia.append(competenciasm.substring(0, 2)).append("/").append(competenciasm.substring(2, 6));
			return competencia.toString();
		}
		return null;
	}
}